<template>
  <div>
      <tabset v-ref:tabs :close="false" style="height: 450px;">
        <tab header='基本信息'>
          <user-essential-info :baseinfo= "data.baseinfo" :modify="modify" v-if="!nonUser" @valid="essentialInfo = true" @invalid="essentialInfo = false"></user-essential-info>
          <no-user-essential-info :baseinfo= "data.baseinfo" :modify="modify" v-if="nonUser" @valid="essentialInfo = true" @invalid="essentialInfo = false"></no-user-essential-info>
        </tab>
        <tab header='表具信息'>
          <!-- <user-meter-info :meterinfo="data.meterinfo" :usertype= "data.baseinfo.base.f_user_type"
          :gasproperties="data.baseinfo.base.f_gasproperties" @valid="meterInfo = true"
          @invalid="meterInfo = false"></user-meter-info> -->
          <!-- <user-meter-info :meterinfo="data.meterinfo" :usertype= "data.baseinfo.base.f_user_type"
          :gasproperties="data.baseinfo.base.f_gasproperties" @self-validator="isDisable()"></user-meter-info> -->
          <user-meter-info :meterinfo="data.meterinfo" :usertype= "data.baseinfo.base.f_user_type"
          :gasproperties="data.baseinfo.base.f_gasproperties"  :metervalid.sync="meterInfo" v-ref:meter :f_userinfo_id="data.baseinfo.base.f_userinfo_id"></user-meter-info>

        </tab>
        <tab header='设备信息'>
          <user-device-info :devicesinfo="data.devicesinfo" :userinfoid='data.baseinfo.base.f_userinfo_id'></user-device-info>
        </tab>
        <tab header='发票及其他信息'>
          <user-paper-info :baseinfo="data.baseinfo"></user-device-info>
        </tab>
      </tabset>
    <div class="form-horizontal auto">
      <div class="form-group">

        <div class="col-sm-offset-7 col-sm-3">
          <!-- || !meterInfo  -->
          <!-- <button type="button" class="btn btn-success" @click="confirm()" :disabled="!(essentialInfo && meterInfo)"> -->
          <button type="button" class="btn btn-success" @click="confirm()" :disabled="!(essentialInfo && meterInfo)">
            保&nbsp;&nbsp;&nbsp;存
          </button>
          <button type="button" class="btn btn-default" @click="cancel()" id='userfiles-useGenInf-取消'>
             取&nbsp;&nbsp;&nbsp;消
          </button>
        </div>
      </div>
    </div>
    <modal :show.sync="show"  v-ref:modal large backdrop="false" >
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="cancel"><span>&times;</span></button>
        <h3 class="modal-title">机表卡信息</h3>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="flex">
          <user-admissibility :data='userinfoid' v-ref:admiss></user-admissibility>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" v-show='false' class="btn btn-primary"  @click='confirm()'>打印</button>
        <!-- <button type="button" class="btn btn-default" @click='cancel()'>取消</button> -->
      </footer>
    </modal>
  </div>
</template>

<script>
import AppData from '../../stores/AppData'
import * as Util from '../Util'
import co from 'co'

let getFileGen = function * (self, data) {
  let result = yield self.$resetpost('rs/logic/getFilesDetail', data, {resolveMsg: null, rejectMsg: '用户详细信息加载失败，请重新选择，如果多次失败，请检查系统是否正常！'})
  self.data = result.data
  // 获取片区信息
  console.log('查询档案信息。。', self.data)
  self.checkout()
}

export default {
  title: '综合信息',
  props: ['row', 'areainfo', 'nonUser', 'modify', 'userinfoid'],
  data () {
    return {
      data: {
        baseinfo: {
          base: {
            f_user_state: '',
            f_user_type: '',
            f_credentials: '',
            f_cost_type: '',
            f_gasproperties: '',
            f_paper_type: ''
          },
          bank: {
            f_bank_name: ''
          }
        },
        meterinfo: [],
        devicesinfo: []
      },
      essentialInfo: false,
      meterInfo: false,
      isNull: false
    }
  },
  ready () {
    // console.log('开始，，，', this.row)
    if (this.row && !this.areainfo) { // 修改用户
      this.areaGen(this.row)
    } else if (!this.row && this.areainfo) { // 小区批量建档
      // 将小区信息转化为综合信息的格式
      this.areainfoInUser(this.areainfo)
    } else if (this.nonUser) {
      this.nonUserGen()
    }
    this.isDisable()
  },
  watch: {
    'row' (val) {
      if (val) {
        this.areaGen(val)
      } else {
        this.nonUserGen()
      }
    },
    'areainfo' (val) {
      if (val) {
        this.$refs.meter.clearMeter()
        this.areainfoInUser(val)
      }
    },
    'data.meterinfo.length' () {
      this.isDisable()
    },
    'nonUser' () {
      // console.log('非民用建档观察...')
      this.nonUserGen()
    }
  },
  methods: {
    isDisable () {
      let is = true
      this.data.meterinfo.forEach((row) => {
        if (!(row.f_meternumber && row.f_meternumber.length > 0)) {
          is = false
        }
      })
      this.isNull = is
    },
    confirm () {
      console.log('档案保存之前，。。。', this.data)
      let hasMachine = false
      for (let i = 0; i < this.data.meterinfo.length; i++) {
        if (this.data.meterinfo[i].gasbrand[0].f_meter_type === '机表') {
          hasMachine = true
          break
        }
      }
      this.$FileService.fileSave(this.data).then((res) => {
        if (this.areainfo) {
          // 小区批量建档
          this.$dispatch('build-success', res.data.result[0])
          this.$refs.meter.clearMeter()
          this.areainfoInUser(this.areainfo)
        } else if (this.nonUser) {
          this.$dispatch('success')
        } else {
          this.$dispatch('success')
        }
        if (hasMachine) {
          this.userinfoid = res.data.result[0].f_userinfo_id
          this.show = true
          this.$refs.admiss.admiss.search(this.userinfoid)
        }
      }).catch((error) => {
        if (error.status === 603) {
          this.$error('重复提交')
          return
        }
        this.$dispatch('error', '档案保存', this.data, error)
      })
    },
    areaGen (data) {
      data.f_userinfo_id = data.f_userinfo_id + ''
      let getFile = getFileGen(this, data)
      co(getFile)
    },
    nonUserGen () {
      this.data = {
        baseinfo: {
          base: {
            f_area: null,
            f_slice_area: null,
            f_street: null,
            f_address: null,
            f_user_state: '正常',
            f_gasproperties: '',
            f_credentials: '',
            f_adjustable: '',
            f_user_type: '非民用',
            f_cost_type: '现金缴费'
          },
          bank: {}
        },
        meterinfo: [{
          gasbrand: '',
          gasmodel: '',
          gasmodeltype: '',
          pricetype: '',
          pricename: '',
          f_inputtor: '',
          f_position: '',
          f_meter_base: 0,
          f_aroundmeter: '左表',
          f_install_date: Util.toStandardTimeString(),
          f_gas_date: Util.toStandardTimeString()
        }],
        devicesinfo: []
      }
      // this.data.baseinfo.base.f_user_type = this.data.baseinfo.base.f_user_type ? [this.data.baseinfo.base.f_user_type] : ''
      this.checkout()
    },
    cancel () {
      this.$dispatch('clean', '取消档案修正', 'user-general-info', this.row)
    },
    checkout () {
      // 对收到的综合信息进行校验（主要目的是为了防止下拉框没有默认值）
      // 对综合用户基本信息进行校验

      this.data.baseinfo.base.f_user_state = this.data.baseinfo.base.f_user_state ? [this.data.baseinfo.base.f_user_state] : ['正常']
      this.data.baseinfo.base.f_user_type = this.data.baseinfo.base.f_user_type ? [this.data.baseinfo.base.f_user_type] : ''
      this.data.baseinfo.base.f_credentials = this.data.baseinfo.base.f_credentials ? [this.data.baseinfo.base.f_credentials] : ['身份证']
      this.data.baseinfo.base.f_cost_type = this.data.baseinfo.base.f_cost_type ? [this.data.baseinfo.base.f_cost_type] : ['现金缴费']
      this.data.baseinfo.base.f_gasproperties = this.data.baseinfo.base.f_gasproperties ? [this.data.baseinfo.base.f_gasproperties] : ''
      this.data.baseinfo.base.f_paper_type = this.data.baseinfo.base.f_paper_type ? [this.data.baseinfo.base.f_paper_type] : ['国税发票']
      this.data.baseinfo.bank.f_bank_name = this.data.baseinfo.bank.f_bank_name ? [this.data.baseinfo.bank.f_bank_name] : ['中国银行']
      if (this.data.baseinfo.base.f_adjustable) {
        this.adjustables.forEach((item) => {
          if (item.label === this.data.baseinfo.base.f_adjustable) {
            this.data.baseinfo.base.f_adjustable = [item.value]
          }
        })
      } else {
        this.data.baseinfo.base.f_adjustable = ''
      }
      if (this.data.baseinfo.base.f_area) {
        this.data.baseinfo.base.f_area = [this.data.baseinfo.base.f_area]
      } else {
        this.data.baseinfo.base.f_area = ''
      }
      if (this.data.baseinfo.base.f_slice_area) {
        this.data.baseinfo.base.f_slice_area = [this.data.baseinfo.base.f_slice_area]
      } else {
        this.data.baseinfo.base.f_slice_area = ''
      }
      // 对表具信息进行校验
      this.data.meterinfo.forEach((row) => {
        if (row.gasbrand) {
          this.meterbrands.forEach((item) => {
            if (item.label === row.gasbrand) {
              row.gasbrand = [item.value]
            }
          })
        } else {
          row.gasbrand = ''
        }
        if (row.gasmodel && (row.gasbrand instanceof Array)) {
          row.gasbrand[0].gasmodel.forEach((item) => {
            if (item.label === row.gasmodel + row.gasmodeltype) {
              row.gasmodel = [item.value]
            }
          })
        } else {
          row.gasmodel = ''
        }
        if (row.f_fire_state === '1') {
          row.f_fire_state1 = true
        } else if (row.f_fire_state === '0') {
          row.f_fire_state1 = false
        }
        // row.f_gas_date_2 = row.f_gas_date_2 ? row.f_gas_date_2 : ''
        // row.f_install_person = row.f_install_person ? [row.f_install_person] : ''
        // console.log('1111111111111', row.f_install_date)
        row.f_install_date = row.f_install_date ? row.f_install_date : ''
        // console.log('11111111111112222', row.f_install_date)
        // row.f_gas_person = row.f_gas_person ? [row.f_gas_person] : ''
        // row.f_gas_person_2 = row.f_gas_person_2 ? [row.f_gas_person_2] : ''
        // console.log('222222222222', row.f_gas_date)
        row.f_gas_date = row.f_gas_date ? row.f_gas_date : ''
        // this.$set('row.f_gas_date', row.f_gas_date ? row.f_gas_date : '')
        // console.log('22222222222233333', row)
        // row.f_changetube_person = row.f_changetube_person ? [row.f_changetube_person] : ''
        row.f_changetube_date = row.f_changetube_date ? row.f_changetube_date : ''
        row.f_aroundmeter = row.f_aroundmeter ? [row.f_aroundmeter] : ''
        row.f_inputtor = row.f_inputtor ? [row.f_inputtor] : ''
        row.f_position = row.f_position ? [row.f_position] : ''
        row.pricetype = row.pricetype ? [row.pricetype] : ''
        // row.pricename = row.pricename ? [row.pricename] : ''
        row.f_user_type = row.f_user_type ? [row.f_user_type] : ''
        row.f_gasproperties = row.f_gasproperties ? [row.f_gasproperties] : ''
        // console.log('3333333333', row)
        if (row.pricename) {
          let names = this.getPricenames(this.data.baseinfo.base.f_user_type,
            this.data.baseinfo.base.f_gasproperties,
            row.pricetype)
          if (names.length > 0) {
            names.forEach((item) => {
              if (item.label === row.pricename) {
                row.pricename = [item.value]
              }
            })
          } else {
            row.pricename = ''
          }
        } else {
          row.pricename = ''
        }
      })
      // console.log('5555555555', this.data.meterinfo)
      // 对设备进行校验
      this.data.devicesinfo.forEach((row) => {
        row.f_devices_type = row.f_devices_type ? [row.f_devices_type] : ''
      })
    },
    getPricenames (f_user_type, f_gasproperties, f_price_type) {
      let rs = []
      if (f_user_type.length === 1 && f_gasproperties.length === 1 && f_price_type.length === 1) {
        rs = AppData.getPrice({f_user_type: f_user_type[0],
          f_gasproperties: f_gasproperties[0], f_price_type: f_price_type[0]})
      }
      return rs
    },
    // 将小区信息转化为综合信息的格式
    areainfoInUser (areainfo) {
      // this.nextTick(() => {
      //
      // })
      this.data = {
        baseinfo: {
          base: {
            f_user_state: '正常',
            f_area: areainfo.f_area,
            f_street: areainfo.f_street,
            f_slice_area: areainfo.f_slice_area,
            f_residential_area: areainfo.f_residential_area,
            f_address: areainfo.f_area_address,
            f_user_type: areainfo.f_user_type,
            f_credentials: '',
            f_cost_type: '',
            f_gasproperties: areainfo.f_gasproperties,
            f_adjustable: areainfo.f_adjustable_name
          },
          bank: {}
        },
        meterinfo: [{
          gasbrand: areainfo.f_meter_brand,
          gasmodel: areainfo.f_meter_style,
          gasmodeltype: areainfo.f_type,
          pricetype: areainfo.f_price_type,
          pricename: areainfo.f_price_name,
          f_inputtor: areainfo.f_inputtor,
          f_position: areainfo.f_position,
          f_aroundmeter: '左表',
          f_meter_base: 0,
          f_install_date: Util.toStandardTimeString(),
          f_gas_date: Util.toStandardTimeString()
        }],
        devicesinfo: []
      }
      this.checkout()
    }
  },
  computed: {
    meterbrands () {
      return AppData.getGasbrand()
    },
    adjustables () {
      return AppData.getAdjustable()
    }
  }
}
</script>
